package assignment3;

import java.util.Date;

public class Pair implements Comparable<Object>{
	private String key;
	private Date viewtime;

	/***
	 * Public constructor.
	 * @param key - String, the key.
	 * @param viewtime - Date, the viewtime.
	 */
	public Pair(String key, Date viewtime) {
		this.key = key;
		this.viewtime = viewtime;
	}
	
	/***
	 * Public empty constructor.
	 */
	public Pair(){
		
	}

	/***
	 * Returns a String object representing this Pair's key.
	 * @return a String representing the key.
	 * @see setkey(String key)
	 */
	public String getKey() {
		return key;
	}

	/***
	 * Sets a String object representing this Pair's key.
	 * @param key - String, the Pair's key.
	 * @see getkey()
	 */
	public void setKey(String key) {
		this.key = key.toString();
	}

	/***
	 * Returns a Date object representing this Pair's viewtime value.
	 * @return a Date representing the viewtime value.
	 * @see setViewtime(Date viewtime)
	 */
	public Date getViewtime() {
		return viewtime;
	}

	/***
	 * Sets a Date object representing this Pair's viewtime value.
	 * @param viewtime - Date, the Pair's viewtime.
	 * @see getkey()
	 */
	public void setViewtime(Date viewtime) {
		this.viewtime = new Date(viewtime.getTime());
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Pair [key=" + key + ", viewtime=" + viewtime + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result
				+ ((viewtime == null) ? 0 : viewtime.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Pair)) {
			return false;
		}
		Pair other = (Pair) obj;
		if (key == null) {
			if (other.key != null) {
				return false;
			}
		} else if (!key.equals(other.key)) {
			return false;
		}
		if (viewtime == null) {
			if (other.viewtime != null) {
				return false;
			}
		} else if (!viewtime.equals(other.viewtime)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(Object o){
		if(o == null){
			throw new IllegalArgumentException("comparable object should not be null");
		}
		if(o instanceof Pair){
			Pair p = (Pair)o;
			if(p.viewtime.equals(viewtime)){
				return 0;
			}
			else if(p.viewtime.before(viewtime)){
				return 1;
			}
			else{
				return -1;
			}
		}else{
			throw new ClassCastException("the object isn't a Pair object");
		}
	}



}
